﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.转速仪
{
     public class zsyfuli:zsybass
     {
         /// <summary>
         /// 打开串口
         /// </summary>
         /// <param name="sSet1">串口设置</param>
         /// <returns>是否成功打开</returns>
         public override bool openCOM(string sSet1)
         {
             bool breturn = base.openCOM(sSet1);
             commex.ReadTimeout = 500;
             commex.ReceivedBytesThreshold = 16;//达到50字节以后才开始触发串口
             commex.DataReceived += comGAS_DataReceived;
             return breturn;
         }

         public override bool SendDeviceCmd()
         {
             byte[] cCmdBuf = new byte[10] { 0x4e, 0x54, 0x30, 0x31, 0x53, 0x04, 0x01, 0x03, 0x00, 0xf7 };
             if (commex.IsOpen)
             {
                 for (int i = 0; i < 3; i++)
                 {
                     commex.Write(cCmdBuf, 0, 10);
                     System.Threading.Thread.Sleep(300);
                 }
             }
             return true;
         }
         void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             if (commex.IsOpen)
             {
                 System.Threading.Thread.Sleep(100);
                 int n = commex.BytesToRead;//
                 byte[] buf = new byte[n];
                 commex.Read(buf, 0, n);
                 if (n > 10)
                 {
                     for (int i = 0; i < n - 11; i++)
                     {
                         if (buf[i + 0] == 0x4E && buf[i + 1] == 0x54 && buf[i + 2] == 0x30 && buf[i + 3] == 0x31 && buf[i + 4] == 0x41 && buf[i + 5] == 0x0D)
                         {
                             int.TryParse(string.Format("{0}", (buf[10 + i] * 256 + buf[11 + i])), out m_iGasturn);
                          

                         }
                     }
                 }
             }
         }
    }
}
